package com.wanchuang.hepos.ui;

/**
 * 文件名 : LocationSelectActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 14:56
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.architecture.utils.ValidateUtils;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.repository.Apis;
import com.wanchuang.hepos.net.NetworkManager;
import com.wanchuang.hepos.net.interfaces.OnRequestCallBack;
import com.wanchuang.hepos.proto.Merchant;
import com.wanchuang.hepos.ui.base.BaseActivity;
import com.wanchuang.hepos.ui.view.easyadapter.BaseViewHolder;
import com.wanchuang.hepos.ui.view.easyadapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import static cn.jiguang.dy.Protocol.mContext;


public class BankSelectActivity extends BaseActivity {

    TextView titleTv;
    RecyclerView locationRv;

    private RecyclerArrayAdapter<Merchant.merchant_basis> mAdapter;
    private String areaId = "0";
    private final List<Merchant.merchant_basis> dataList = new ArrayList<>();  // 请求中数据

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_select);
        titleTv = findViewById(R.id.title_tv);
        locationRv = findViewById(R.id.location_rv);
        findViewById(R.id.back_iv).setOnClickListener(v -> finish());
        titleTv.setText("银行");
        initRecycle();
        loadData();
    }

    private void loadData() {
        // 加载底部数据
        showProgress();
        Merchant.get_mcc.Builder builder = Merchant.get_mcc.newBuilder();
        builder.setType(areaId);
        NetworkManager.INSTANCE.post(Apis.get_bank_list, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                hideProgress();
                if (titleTv == null) return; // 防止请求延迟出现闪退
                List<Merchant.merchant_basis> list = Merchant.get_mcc.parseFrom(response).getMccListList();
                if (!ValidateUtils.isValidate(list)) {
                    showShortToast("没有更多了");
                    return;
                }
                dataList.clear();
                mAdapter.clear();
                dataList.addAll(list);
                mAdapter.addAll(list);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
                showShortToast(errorMessage);
            }

        });

    }

    /**
     * y
     * 实例化底部RecyclerView
     * *
     */
    private void initRecycle() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        locationRv.setLayoutManager(layoutManager);
        locationRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        locationRv.setAdapter(mAdapter = new RecyclerArrayAdapter<Merchant.merchant_basis>(mContext) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_location, parent, false));
            }

            class ViewHolder extends BaseViewHolder<Merchant.merchant_basis> {
                TextView location;

                public ViewHolder(View itemView) {
                    super(itemView);
                    location = itemView.findViewById(R.id.tv_location);
                }

                @Override
                public void setData(Merchant.merchant_basis data) {
                    location.setText(data.getName());
                }
            }
        });
        mAdapter.setOnItemClickListener(position -> {
            Merchant.merchant_basis locationBean = dataList.get(position);
            areaId = locationBean.getCode();
            mSharedViewModel.bankName.setValue(areaId+","+locationBean.getName());
            finish();
        });
    }
}
